<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
    <!-- 添加Facebook小游戏SDK -->
    <script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></script>
  </head>
  <body>
    <div id="unity-container" class="unity-desktop">
      <canvas id="unity-canvas"></canvas>
      <div id="unity-loading-bar">
        <div id="unity-logo"></div>
        <div id="unity-progress-bar-empty">
          <div id="unity-progress-bar-full"></div>
        </div>
      </div>
      <div id="unity-mobile-warning">
        WebGL builds are not supported on mobile devices.
      </div>
      <div id="unity-footer">
        <div id="unity-webgl-logo"></div>
        <div id="unity-fullscreen-button"></div>
        <div id="unity-build-title">{{{ PRODUCT_NAME }}}</div>
      </div>
    </div>
    <script>
      var buildUrl = "Build";
      var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
      var config = {
        dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
        frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
        codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
#if MEMORY_FILENAME
        memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
#endif
#if SYMBOLS_FILENAME
        symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
#endif
        streamingAssetsUrl: "StreamingAssets",
        companyName: "{{{ COMPANY_NAME }}}",
        productName: "{{{ PRODUCT_NAME }}}",
        productVersion: "{{{ PRODUCT_VERSION }}}",
      };

      var container = document.querySelector("#unity-container");
      var canvas = document.querySelector("#unity-canvas");
      var loadingBar = document.querySelector("#unity-loading-bar");
      var progressBarFull = document.querySelector("#unity-progress-bar-full");
      var fullscreenButton = document.querySelector("#unity-fullscreen-button");
      var mobileWarning = document.querySelector("#unity-mobile-warning");

      // 在移动设备上，Unity会在这里显示警告
      // if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
      //   container.className = "unity-mobile";
      //   config.devicePixelRatio = 1;
      //   mobileWarning.style.display = "block";
      //   setTimeout(() => {
      //     mobileWarning.style.display = "none";
      //   }, 5000);
      // } else {
      //   canvas.style.width = "{{{ WIDTH }}}px";
      //   canvas.style.height = "{{{ HEIGHT }}}px";
      // }
      // 改成unity-mobile，使之全屏显示，否则默认是横屏
      container.className = "unity-mobile";
      config.devicePixelRatio = 1;

#if BACKGROUND_FILENAME
      canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
#endif
      loadingBar.style.display = "block";

      // 把加载Unity游戏代码和素材的代码段提取到一个function中，便于后续调用
      function startUnity(){
        var script = document.createElement("script");
        script.src = loaderUrl;
        script.onload = () => {
          createUnityInstance(canvas, config, (progress) => {
            progressBarFull.style.width = 100 * progress + "%";
          }).then((unityInstance) => {
            loadingBar.style.display = "none";
            fullscreenButton.onclick = () => {
              unityInstance.SetFullscreen(1);
            };
          }).catch((message) => {
            // alert(message); // facebook 不允许alert
            console.error("Creating Unity Instance Failed: ", message);
          });
        };
        document.body.appendChild(script);
      }

      // 启动 facebook初始化
      console.log("=====> FBInstant.initializeAsync");
      FBInstant.initializeAsync()
        .then(function () {
          console.info("=====> FBInstant.startGameAsync");
          // 告诉Facebook加载完毕，可以进入游戏了
          // 也可以在这里加载Unity游戏代码和资源，然后再加载完后再进入游戏         
          FBInstant.startGameAsync().then(function() {
            console.info("=====> screen size: " + `${screen.width} x ${screen.height}`);
            console.info("=====> window size: " + `${window.innerWidth} x ${window.innerHeight}`)
            
            // 开始加载Unity游戏代码和资源
            startUnity();
          });
        });
    </script>
  </body>
</html>
